<template>
  <div>
    <!-- 筛选条件 -->
    <el-form style="margin: 10px" size="mini" :inline="true" :model="selectfightchange" class="demo-form-inline">
      <el-form-item label="航空公司">
        <el-select filterable clearable size="mini" v-model="selectfightchange.airline" placeholder="请选择">
          <el-option v-for="item in airportLists" :key="item.airlinerId" :label="item.airlinerId+item.airliner"
            :value="item.airlinerId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出票渠道">
        <el-select size="mini" v-model="selectfightchange.ticketingChannel" placeholder="请选择" clearable>
          <el-option v-for="item in TicketChannelList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否生成PNR">
        <el-select size="mini" v-model="selectfightchange.pnr" placeholder="请选择" clearable>
          <el-option v-for="item in iscreatpnr" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker :value-format="format" size="mini" v-model="selectfightchange.startingTime" type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker :value-format="format" size="mini" v-model="selectfightchange.endTime" type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="seachBtn">查询</el-button>
        <el-button type="primary" @click="addDialogVisible = true">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表区域 -->
    <!-- 列表区域 -->
    <el-table :header-cell-style="{
        color: '#fff',
        background: '#6090EC',
        fontSize: '11px',
        fontWeight: 'normal',
      }" id="tableTop" :data="ChannelRulelist" border stripe size="mini">
      <el-table-column label="编号" prop="ruleId"></el-table-column>
      <el-table-column label="航空公司" prop="airlinerId"></el-table-column>
      <el-table-column label="舱位" prop="cabin"></el-table-column>
      <el-table-column label="出票渠道" prop="peksourcetype">
        <template slot-scope="scope">
          <template v-if="scope.row.peksourcetype == '2'">B2B</template>
          <template v-if="scope.row.peksourcetype == '1'">BSP</template>

        </template>
      </el-table-column>
      <!-- <el-table-column label="异地出票途径" prop="ticketWay">
        <template slot-scope="scope">
          <template v-if="scope.row.ticketWay == 'LinkStar'">航信</template>
          <template v-if="scope.row.ticketWay == 'Dove'">德付通</template>
          <template v-if="scope.row.ticketWay == 'Huifu'"
            >汇付天下出票</template
          >
          <template v-if="scope.row.ticketWay == 'Yeebao'">易宝出票</template>
        </template>
      </el-table-column> -->
      <el-table-column label="开始时间" prop="starttime">
        <template slot-scope="scope">
          {{ showcreatTime(scope.row.starttime) }}
        </template>
      </el-table-column>

      <el-table-column label="结束时间" prop="endtime">
        <template slot-scope="scope">
          {{ showcreatTime(scope.row.endtime) }}
        </template>
      </el-table-column>
      <el-table-column label="是否生成PNR" prop="isCreatePnr">
        <template slot-scope="scope">
          <template v-if="scope.row.isCreatePnr == 1">否</template>
          <template v-if="scope.row.isCreatePnr == 2">是</template>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editchanneRule(scope.row)">
            修改
          </el-button>
          <el-button type="primary" size="mini" @click="delchanneRule(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="45%" center>
      <el-form style="margin: 10px" size="mini" :inline="true" :model="adddata" class="demo-form-inline">
        <el-form-item label="航空公司">
          <el-select clearable size="mini" v-model="adddata.airline" placeholder="请选择">
            <el-option v-for="item in airportLists" :key="item.airlinerId" :label="item.airliner"
              :value="item.airlinerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="北京出票渠道">
          <el-select size="mini" v-model="adddata.ticketingChannel" placeholder="请选择" clearable>
            <el-option v-for="item in TicketChannelList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异地航班出票渠道" v-if="this.adddata.ticketingChannel == '1'">
          <el-select size="mini" v-model="adddata.remoteFlight" placeholder="请选择" clearable>
            <el-option v-for="item in remoteFlightList1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异地航班出票渠道" v-if="this.adddata.ticketingChannel == '2'">
          <el-select size="mini" v-model="adddata.remoteFlight" placeholder="请选择" clearable>
            <el-option v-for="item in remoteFlightList2" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否生成pnr">
          <el-select clearable size="mini" v-model="adddata.pnr" placeholder="请选择">
            <el-option v-for="item in iscreatpnr" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用时间">
          <el-date-picker :value-format="format" size="mini" v-model="adddata.start" type="date" placeholder="选择日期">
          </el-date-picker>-<el-date-picker :value-format="format" size="mini" v-model="adddata.finish" type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="适用舱位">
          <el-input size="mini" v-model="adddata.position" placeholder="请输入大写英文字母"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="getaddFlightChannelRule()">保 存</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog title="修改" :visible.sync="editDialogVisible" :close-on-click-modal="false" width="45%" center>
      <el-form style="margin: 10px" size="mini" :inline="true" :model="editdata">
        <el-form-item label="航空公司">
          <el-select size="mini" v-model="editdata.airlinerId" placeholder="请选择" clearable>
            <el-option v-for="item in airportLists" :key="item.airlinerId" :label="item.airliner"
              :value="item.airlinerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="北京出票渠道">
          <el-select size="mini" v-model="editdata.ticketingChannel" placeholder="请选择" clearable>
            <el-option v-for="item in TicketChannelList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异地航班出票渠道" v-if="editdata.ticketingChannel == '1'">
          <el-select size="mini" v-model="editdata.remoteFlight" placeholder="请选择" clearable>
            <el-option v-for="item in remoteFlightList1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异地航班出票渠道" v-if="editdata.ticketingChannel == '2'">
          <el-select size="mini" v-model="editdata.remoteFlight" placeholder="请选择" clearable>
            <el-option v-for="item in remoteFlightList2" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否生成pnr">
          <el-select clearable size="mini" v-model="editdata.pnr" placeholder="请选择">
            <el-option v-for="item in iscreatpnr" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用时间">
          <el-date-picker :value-format="format" size="mini" v-model="editdata.starttime" type="date"
            placeholder="选择日期">
          </el-date-picker>-<el-date-picker :value-format="format" size="mini" v-model="editdata.endtime" type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="适用舱位">
          <el-input size="mini" v-model="editdata.position" placeholder="请输入大写英文字母"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="geteditFlightChannelRule()">保 存</el-button>
      </span>
    </el-dialog>
    <!-- 删除 -->
    <el-dialog title="确认删除" :close-on-click-modal="false" :visible.sync="delDialogVisible" width="30%" center>
      <span>确认删除此条出票规定吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="getruledelRemove()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 查询条件数据
        selectfightchange: {
          airline: "", //航空公司
          ticketingChannel: "", //出票渠道
          pnr: "", //是否生成PNR
          startingTime: "", //开始时间
          endTime: "", //结束时间
        },
        // 新增数据
        adddata: {
          airline: "", //航空公司二字码
          ticketingChannel: "", //北京出票渠道
          remoteFlight: "", //异地航班出票渠道
          pnr: "", //是否生成pnr
          start: "", //适用时间前
          finish: "", //时间
          position: "", //适用仓位
        },
        // 修改数据
        editdata: {
          airlinerId: "", //航空公司二字码
          ticketingChannel: "", //北京出票渠道
          remoteFlight: "", //异地航班出票渠道
          pnr: "", //是否生成pnr
          starttime: "", //适用时间
          endtime: "", //时间
          position: "", //适用仓位
          ruleId: "",
        },

        // 北京出票渠道
        TicketChannelList: [{
            value: "1",
            label: "BSP",
          },
          {
            value: "2",
            label: "B2B",
          },
        ],
        // 异地出票渠道
        remoteFlightList1: [{
            value: "LinkStar",
            label: "航信",
          },
          {
            value: "Dove",
            label: "德付通",
          },
        ],
        remoteFlightList2: [{
            value: "Huifu",
            label: "汇付天下出票",
          },
          {
            value: "Yeebao",
            label: "易宝出票",
          },
        ],

        // 是否生成pnr
        iscreatpnr: [{
            value: "true",
            label: "是",
          },
          {
            value: "false",
            label: "否",
          },
        ],
        // 新增弹窗显示隐藏
        addDialogVisible: false,
        // 修改
        editDialogVisible: false,
        // 删除
        delDialogVisible: false,
        format: "yyyy-MM-dd",
        // 分页
        pageSize: 10,
        currentPage: 1,
        pageTotal: 0,
        // 航司列表
        airportLists: [],
        // 列表返回数据
        ChannelRulelist: [],
      };
    },
    watch: {},

    methods: {
      // 时间显示
      showcreatTime(val) {
        var val1 = val.split(".");
        var val2 = val1[0].split("T").join(" ");
        return val2;
      },
      //分页
      handleSizeChange(value) {
        console.log("1", value);
        localStorage.setItem("pageSize", value);
        localStorage.setItem("pageNum", 1);
        this.getselectFlightChannelRuleBy();
        this.pageSize = value;
      },
      handleCurrentChange(value) {
        console.log("2", value);

        localStorage.setItem("pageNum", value);
        localStorage.setItem("pageSize", this.pageSize);
        this.currentPage = value;

        this.getselectFlightChannelRuleBy();
        // orders = res.data;
      },
      search() {
        localStorage.setItem("pageNum", this.currentPage);
        localStorage.setItem("pageSize", 10);
        this.getselectFlightChannelRuleBy();
      },
      // 航司列表接口
      getairportList() {

        this.$axios.getqueryAirlinerSelect({}).then((res) => {
          console.log(res);
          // debugger

          if (res.code == "200") {
            this.airportLists = res.data;
          }
        });
      },
      // 出票规定列表
      getselectFlightChannelRuleBy() {

        this.$axios
          .getselectFlightChannelRuleBy({
            airline: this.selectfightchange.airline, //航空公司
            ticketingChannel: this.selectfightchange.ticketingChannel, //出票渠道
            pnr: this.selectfightchange.pnr, //pnr
            startingTime: this.selectfightchange.startingTime, //开始时间
            endTime: this.selectfightchange.endTime, //结束时间
          })
          .then((res) => {
            console.log(res);
            // debugger
            if (res.code == "200") {
              this.ChannelRulelist = res.data;
              this.pageTotal = res.total;
            }
          });
      },
      // 新增出票规定
      getaddFlightChannelRule() {
        if (
          this.adddata.airline != "" &&
          this.adddata.ticketingChannel != "" &&
          this.adddata.remoteFlight != "" &&
          this.adddata.pnr != "" &&
          this.adddata.start != "" &&
          this.adddata.finish != "" &&
          this.adddata.position != ""
        ) {
          localStorage.setItem("isToken", true);
          this.$axios
            .getaddFlightChannelRule({
              airline: this.adddata.airline, //航空公司二字码
              ticketingChannel: this.adddata.ticketingChannel, //北京出票渠道
              remoteFlight: this.adddata.remoteFlight, //异地航班出票渠道
              pnr: this.adddata.pnr, //是否生成pnr
              start: this.adddata.start, //适用时间前
              finish: this.adddata.finish, //适用时间后
              position: this.adddata.position, //适用仓位
            })
            .then((res) => {
              console.log(res);
              // debugger
              if (res.code == "200") {
                this.addDialogVisible = false;
                this.getselectFlightChannelRuleBy();

                this.$message({
                  message: "新增成功",
                  type: "success",
                });
              }
              
            });
        } else {
          this.$message.error("新增渠道规定选项均为必填项，请认真填写");
        }
      },
      // 删除提醒点击事件
      delchanneRule(row) {
        console.log(row);
        this.delDialogVisible = true;
        this.delruleId = row.ruleId;
      },
      seachBtn() {
        console.log(111)
        localStorage.setItem("pageSize", 10);
        localStorage.setItem("pageNum", 1);
        this.getselectFlightChannelRuleBy()
      },
      //删除出票规定
      getruledelRemove() {
        this.$axios
          .getruledelRemove({
            ruleId: this.delruleId,
          })
          .then((res) => {
            console.log(res);
            // debugger
            if (res.code == "200") {
              this.getselectFlightChannelRuleBy();
              this.delDialogVisible = false;
              this.$message({
                message: "删除成功",
                type: "success",
              });
            }
          });
      },
      // 修改点击事件
      editchanneRule(row) {
        console.log(row);
        this.editdata = row;
         this.editdata.airlinerId=row.airlinerId, //航空公司二字码
          this.editdata.ticketingChannel=row.peksourcetype
          this.editdata.remoteFlight=row.ticketWay, //异地航班出票渠道
          this.editdata.pnr=row.pnr, //是否生成pnr
          this.editdata.starttime=row.starttime, //适用时间
          this.editdata.endtime=row.endtime, //时间
          this.editdata.position=row.cabin, //适用仓位
          this.editdata.ruleId=row.ruleId,
        this.editDialogVisible = true;
      },
      // 查询待修改数据

      getrulegetInfo(ruleid) {
        this.$axios
          .getrulegetInfo({
            ruleId: ruleid,
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {
              this.editdata = res.data;
              console.log(this.editdata);
            }
          });
      },
      // 修改出票规定
      geteditFlightChannelRule() {
        if (
          this.editdata.airline != "" &&
          this.editdata.ticketingChannel != "" &&
          this.editdata.remoteFlight != "" &&
          this.editdata.pnr != "" &&
          this.editdata.start != "" &&
          this.editdata.finish != "" &&
          this.editdata.position != ""
        ) {
          localStorage.setItem("isToken", true);
          this.$axios
            .getaddFlightChannelRule({
              airline: this.editdata.airliner, //航空公司二字码
              ticketingChannel: this.editdata.ticketingChannel, //北京出票渠道
              remoteFlight: this.editdata.remoteFlight, //异地航班出票渠道
              pnr: this.editdata.pnr, //是否生成pnr
              start: this.editdata.starttime, //适用时间前
              finish: this.editdata.endtime, //适用时间后
              position: this.editdata.position, //适用仓位
              id: this.editdata.ruleId,
            })
            .then((res) => {
              console.log(res);
              // debugger
              if (res.code == "200") {
                this.editDialogVisible = false;
                this.getselectFlightChannelRuleBy();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              }
              
            });
        } else {
          this.$message.error("新增渠道规定选项均为必填项，请认真填写");
        }
      },
    },

    mounted() {
      // 列表
      this.getselectFlightChannelRuleBy();
      this.getairportList();
    },
  };
</script>